﻿
Imports Microsoft.Office.Interop.Outlook

Namespace UI
    Public Class pmCopyMailForm
        Inherits PMOutlookAddIn.UI.pmBaseFolderTreeMailForm


        Public Sub New(ByVal App As Application, ByVal mailToWorkWith As MailItem)
            MyBase.New(App, mailToWorkWith)
            InitializeComponent()
            btnDoAction.Text = "Verzenden en opslaan"
        End Sub

        Private Sub pmCopyMailForm_ActionButtonClicked() Handles Me.ActionButtonClicked
            Dim destinationFolder As Outlook.MAPIFolder = _pmFoldertree.GetSelectedFolder()

            If IsNothing(destinationFolder) Then
                Return
            End If

            Dim mailCopier As IMailManager = New MailManager(_app, _mailToWorkWith)
            mailCopier.CopyTo(destinationFolder)
            SharedMethods.SetCurrentFolder(_app, destinationFolder)
            Me.Close()
        End Sub

        'Private Sub pmCopyMailForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '    SelectRelevantFolder(getSearchString)
        'End Sub

        Public Overrides Function getSearchString() As String
            'zoeken naar dpnummer
            Dim dpNummer As String = SharedFunctions.GetDPNumberOutOfString(_mailToWorkWith.Subject)
            If dpNummer <> String.Empty Then
                Return dpNummer
            End If

            'zoeken op eerste contact van de lijst van ontvangers
            If _mailToWorkWith.Recipients.Count > 0 Then
                Dim firstRecipient As Recipient = _mailToWorkWith.Recipients(1)

                Dim fullname As String = SharedFunctions.contactFullnameByEmail(_app, firstRecipient.Address)
                If fullname <> String.Empty Then
                    Return fullname
                End If
            End If

            Return String.Empty
        End Function

        Public Overrides Sub pmForm_Shown(ByVal sender As Object, ByVal e As System.EventArgs)
            SharedMethods.ControlSetFocus(btnDoAction)
        End Sub


        Public Overrides Function getEmail() As String
            If _mailToWorkWith.Recipients.Count > 0 Then
                Dim firstRecipient As Recipient = _mailToWorkWith.Recipients(1)

                Return firstRecipient.Address
            End If
            Return String.Empty
        End Function

    End Class
End Namespace